$(document).ready(function () {
  // Header Submenu
  setupHeaderSubmenu();

  //暫時用來判斷是否為首頁用header
  var path = location.pathname;
  if (
    path === "/demo/orientalfs/" ||
    path === "/demo/orientalfs/index.php" ||
    path === "/index.php" ||
    path === "/"
  ) {
    $(".headerwrap").removeClass("regular");
  } else {
    $(".headerwrap").addClass("regular");
  }

  // init wow animation
  wow = new WOW({
    boxClass: "wow",
    animateClass: "animated",
    offset: 0,
    mobile: true,
    live: true,
  });
  wow.init();

  // node-waves animation
  if (Waves) {
    Waves.init({ duration: 1000 });
    Waves.attach(".btn", ["waves-float"]);
  }

  // submenu click function
  function setupHeaderSubmenu() {
    if ($(window).width() < 992) {
      $(".headerwrap .has_submenu > a").on("click", function (e) {
        e.preventDefault();
        $(this).siblings(".submenu").slideToggle();
        $(this).toggleClass("active");
      });
    }
  }
  // toggleopen click function
  $(".toggleopen").on("click", function (e) {
    e.preventDefault();
    $(this).toggleClass("opened");
    $(this).next().slideToggle();
  });

  // 點擊空白處關閉選單
  $(document).on("click", function (e) {
    var menu_opened = $("#navbar").hasClass("show");
    if (
      !$(e.target).closest("#navbar").length &&
      !$(e.target).is("#navbar") &&
      menu_opened === true
    ) {
      $("#navbar").collapse("toggle");
    }
  });

  // set page height
  var topbottomh = $(".topbar").outerHeight() + $(".footer").outerHeight();
  $(".pagecont").css("min-height", "calc(100vh - " + topbottomh + "px)");

  // home slider setting
  $(".mainslider").slick({
    dots: false,
    arrows: true,
    autoplay: true,
    autoplaySpeed: 4000,
    infinite: true,
    speed: 4000,
    slidesToShow: 1,
    adaptiveHeight: true,
    fade: true,
    responsive: [
      {
        breakpoint: 530,
        settings: {
          arrows: false,
        },
      },
    ],
  });

  $(".nav-slider").slick({
    dots: false,
    arrows: true,
    autoplay: false,
    infinite: true,
    speed: 800,
    slidesToShow: 5,
    adaptiveHeight: true,
    responsive: [
      {
        breakpoint: 912,
        settings: {
          slidesToShow: 3,
        },
      },
      {
        breakpoint: 576,
        settings: {
          slidesToShow: 1,
        },
      },
    ],
  });

  solutionInit();
  $(".solution-slider").slick({
    autoplay: true,
    autoplaySpeed: 5000,
    pauseOnHover: false,
    speed: 800,
    slidesToShow: 1,
    slidesToScroll: 1,
    arrows: false,
    fade: true,
    asNavFor: ".solution-nav",
    responsive: [
      {
        breakpoint: 576,
        settings: {
          arrows: true,
        },
      },
    ],
  });
  $(".solution-nav").slick({
    slidesToShow: 3,
    slidesToScroll: 1,
    asNavFor: ".solution-slider",
    dots: false,
    focusOnSelect: true,
    centerMode: true,
  });
  function solutionInit() {
    $(".solution-nav")
      .on("afterChange init", function (event, slick, direction) {
        // remove all prev/next
        slick.$slides.removeClass("prevdiv").removeClass("nextdiv");
        // find current slide
        for (var i = 0; i < slick.$slides.length; i++) {
          var $slide = $(slick.$slides[i]);
          if ($slide.hasClass("slick-current")) {
            // update DOM siblings
            $slide.prev().addClass("prevdiv");
            $slide.next().addClass("nextdiv");
            break;
          }
        }
      })
      .on("beforeChange", function (event, slick) {
        // optional, but cleaner maybe
        // remove all prev/next
        slick.$slides.removeClass("prevdiv").removeClass("nextdiv");
      });
  }

  var picPopup = $(".popup");
  // pictures popup
  picPopup.magnificPopup({
    type: "image",
    tLoading: "Loading ...",
    gallery: {
      enabled: true,
      navigateByImgClick: true,
      preload: [0, 1], // Will preload 0 - before current, and 1 after the current image
    },
  });

  // scroll functions
  var menuh = $(".topbar").outerHeight() - 1;
  $(".pagebanner").css("margin-top", menuh);

  $(window).on("scroll", function () {
    if ($(window).scrollTop() > menuh) {
      $(".headerwrap").addClass("fixed");
      // $(".headerwrap").css("padding-top", menuh);
    } else {
      $(".headerwrap").removeClass("fixed");
      // $(".headerwrap").css("padding-top", 0);
    }
  });

  $(".scrollToTop").on("click", function (e) {
    e.preventDefault();
    $("html, body").animate({ scrollTop: 0 }, 800);
    return false;
  });

  //換頁控制處理
  $("[data-link]").click(function () {
    let url = $(this).attr("data-link");
    if (url) {
      location.href = url;
    }
  });

  //另開新頁處理
  $(document).on("click", "[data-url]", function () {
    let url = $(this).attr("data-url");
    if (url) {
      window.open(url);
    }
  });
  // $("[data-url]").click(function(){
  //   let url = $(this).attr('data-url');
  //   if(url){
  //     window.open(url);
  //   }
  // });

  //另開視窗處理
  $("[data-openurl]").click(function () {
    let url = $(this).attr("data-openurl");
    if (url) {
      window.open(url, "", (config = "height=500,width=500"));
    }
  });
});
